class Solution {
public:
    string longestPalindrome(string s) {
        int n = s.size();
        vector<vector<bool>> dp(n, vector<bool>(n));

        int len = 1, begin = 0;
        for (int i = n; i >= 0; i--)
        {
            for (int j = i; j < n; j++)
            {
                if (s[i] == s[j])
                {
                    if (i == j || i + 1 == j)
                        dp[i][j] = true;
                    else
                        dp[i][j] = dp[i + 1][j - 1];

                    if (dp[i][j] == true && j - i + 1 > len)
                        len = j - i + 1, begin = i;
                }
            }
        }
        return s.substr(begin, len);
    }
};